/**
 * Created by hqh on 2016/8/8.
 */
var loadBool = true, //列表数据请求是否完成标识
        perPage = 15;       //列表每页显示条数

//页面初始化
$(function () {
//    showRecommendList();

    // 查询关注列表
    $('#query').click(function () {
        showRecommendList();
    });
});

//显示关注列表 从第1条记录开始
function showRecommendList() {
    $("#curPage").text(1);
    $("#pages").text(1);
    getCurPage(1);
}

//获取当前活动页的关注列表数据
function getCurPage(curPage) {
    if (!loadBool) {
        return;
    } else {
        loadBool = false;
        $('#recommendList').empty();
        var loadingIndex = layer.load(2, {
            shade: [0.1, '#fff']    //0.1透明度的白色背景
        });
        var data = JSON.stringify({
            'start_pos': (curPage - 1) * perPage,
            'limit': perPage,
            'own_node': $('#ownNode option:selected').val(),
            'om_name': $('#mCade').val(),
            'status': $('#status option:selected').val()
        });
        console.log('data');
        /*
         $.ajax({
         type: "POST",
         url: "",
         data:data,
         dataType: "json",
         success: function (res){
         console.log(res);
         layer.close(loadingIndex);
         if(res.status==0){
         console.log('推荐列表：'+JSON.stringify(res));
         var pageNum = Math.ceil(res.result.total/perPage);
         $("#pages").text(pageNum);
         $('#curPage').text(curPage);
         for(var i=0, list=res.result.list; i<list.length; i++){
         for(var key in list[i]){
         if(list[i][key] == undefined || list[i][key] == null){
         list[i][key] = " ";
         }
         }
         var item = '<tr>' +
         '<td><input type="checkbox" /></td>' +
         '<td>'+list[i].id+'</td>' +
         '<td>'+list[i].own_node+'</td>' +
         '<td>'+list[i].om_name+'</td>' +
         '<td>'+list[i].valid_start_time+'——'+list[i].valid_end_time+'</td>' +
         '<td>'+list[i].own_type+'</td>' +
         '<td>'+list[i].status+'</td>' +
         '<td>' +
         '<a class="check" href="javascript:void(0);" title="">查看</a>' +
         '<a class="modify" href="javascript:void(0);" title="">修改</a>' +
         '<a class="switch invalid" href="javascript:void(0);" title="">启用</a>' +
         '<a class="del" href="javascript:void(0);" title="">删除</a>' +
         '</td>' +
         '</tr>';
         $('#recommendList').append(item);
         }
         }
         else{
         bbx_alert_error(data.message);
         }
         loadBool = true;
         },
         error: function (xhr, status, error) {
         layer.close(loadingIndex);
         bbx_alert_error('出错啦~');
         loadBool = true;
         }
         });
         */
    }
}

$(document).ready(function () {
    //显示上一页
    $('#lastPage').click(function () {
        var curPage = parseInt($("#curPage").text());
        if (curPage <= 1) {
            return;
        } else {
            getCurPage(curPage - 1);
        }
    });

    //显示下一页
    $('#nextPage').click(function () {
        var curPage = parseInt($("#curPage").text()),
                totalpage = parseInt($('#pages').text());
        if (curPage == totalpage) {
            return;
        } else {
            getCurPage(curPage + 1);
        }
    });

    //查看关注 弹框
    $('#recommendList').delegate('.check', 'click', function () {
        var id = 1,
                content = '<div><h3>查看</h3><p>内容待定...</p></div>';
        layer.open({
            type: 1,
            skin: 'layui-layer-rim',
            area: ['320px, auto'],
            title: '查看',
            content: content,
            success: function (layero, index) {
                checkRecommend(layero, index, id);
            }
        });
    });

    //修改关注 弹框
    $('#recommendList').delegate('.modify', 'click', function () {
        var id = 1,
                content = '<div><h3>修改</h3><p>内容待定...</p></div>';
        layer.open({
            type: 1,
            skin: 'layui-layer-rim',
            area: ['320px, auto'],
            title: '修改',
            content: content,
            success: function (layero, index) {
                modifyRecommend(layero, index, id);
            }
        });
    });

    //启用 关闭 按钮
    $('#recommendList').delegate('.switch', 'click', function () {
        var id = 1,
                _this = this;
        if ($(this).hasClass('invalid')) {
            bbx_confirm2('确认启用?', function () {
                enableRecommend(id, _this);
            }, function () {
                //do nothing
            });
        } else {
            bbx_confirm2('确认关闭?', function () {
                disableRecommend(id, _this);
            }, function () {
                //do nothing
            });
        }
    });

    //删除 按钮
    $('#recommendList').delegate('.del', 'click', function () {
        var id = $(this).parents('tr').find('td').eq(0).text(),
                _this = this;
        bbx_confirm2('确认删除此条记录?', function () {
            delRecommend(id, _this);
        }, function () {
            //do nothing
        });
    });

    //添加关注 弹出页面
    $('#addRecommend').click(function () {
        var index = layer.open({
            type: 2,
            title: false,
            closeBtn: 0, //不显示关闭按钮
            maxmin: false,
            content: '/recommendmanager/add_view',
            success: function (layero, index) {
                initAddRecommend(layero);
            }
        });
        layer.full(index);
    });
});


//查看关注
function checkRecommend(layero, index, id) {
    //ajax......
}

//修改关注
function modifyRecommend(layero, index, id) {
    //ajax......
}

//启用
function enableRecommend(id, target) {
    $(target).removeClass('invalid').text('关闭');
    //ajax......
}
//关闭
function disableRecommend(id, target) {
    $(target).addClass('invalid').text('启用');
    //ajax......
}

//删除
function delRecommend(id, target) {
    $(target).parents('tr').remove();
    //ajax......
}

//添加关注 初始页面
function initAddRecommend(layero) {
    var layerIframe = layero.find('iframe:first-child').contents();
    back(layerIframe);
    addRecommend(layerIframe);
}

//返回上一页 关闭添加关注页面
function back(layerIframe) {
    layerIframe.on('click', "#back", function () {
        layer.closeAll();
    });
}

//添加关注 执行函数
function addRecommend(layerIframe) {
    layerIframe.find('#save').click(function () {
        layer.closeAll();
        //ajax......
    });
}

$(document).ready(function () {
    // 全选反选, 选择
    $('table thead,#checkAll').click(function () {
        checkAll('#checkAll');
    });
    $('#recommendList').delegate('tr', 'click', function () {
        var target = $(this).find('input[type="checkbox"]').get();
        checkItem(target);
    });
    $('#recommendList').delegate('input[type="checkbox"]', 'click', function () {
        checkItem(this);
    });

    function checkAll(target) {
        if ($(target).prop('checked')) {
            $(target).prop('checked', false);
            $('#recommendList tr input[type="checkbox"]').prop('checked', false);
        } else {
            $(target).prop('checked', true);
            $('#recommendList tr input[type="checkbox"]').prop('checked', true);
        }
    }
    function checkItem(target) {
        if ($(target).prop('checked')) {
            $(target).prop('checked', false);
            $('#checkAll').prop('checked', false);
        } else {
            $(target).prop('checked', true);
        }
    }
});
